package com.ygqh.baby.controller.admin;

import com.alibaba.fastjson.JSON;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.handler.HomePageProductHandler;
import com.ygqh.baby.model.HomePageProduct;
import com.ygqh.baby.model.HomeProductData;
import com.ygqh.baby.po.AdminUser;
import com.ygqh.baby.shiro.utils.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin/hpp")
public class HomePageProductController extends BaseController<Object, Long> {

    @Autowired
    private HomePageProductHandler homePageProductHandler;

    @RequestMapping("setHomePageProduct")
    @ResponseBody
    public Message setHomePageProduct(@RequestBody List<HomeProductData> products) {
        AdminUser user = SessionUtil.getCurrentUser();
        HomePageProduct homePageProduct = new HomePageProduct();
        homePageProduct.setpList(products);
        homePageProduct.setHandler(user.getTrueName());
        homePageProduct.setUpdateTime(new Date());

        return homePageProductHandler.setHomePageProduct(homePageProduct);
    }

    @RequestMapping("getHomePageProductInfo")
    @ResponseBody
    public Message getHomePageProduct() {
        HomePageProduct homePageProduct = homePageProductHandler.getHomePageProductInfo();
        return Message.success(homePageProduct);
    }

    public static void main(String[] args) {
        HomePageProduct homePageProduct = new HomePageProduct();
        HomeProductData product = new HomeProductData();

        List<HomeProductData> productList = new ArrayList<>();
        product.setSortNum(1);
        product.setProductId(95370L);
        HomeProductData product2 = new HomeProductData();
        product2.setSortNum(2);
        product2.setProductId(95369L);

        productList.add(product);
        productList.add(product2);
        homePageProduct.setpList(productList);
        System.out.println(JSON.toJSONString(productList));

    }

}
